// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using ValidationAspects.Core;
using ValidationAspects.Sdk;

namespace ValidationAspects
{
	public static class TypeValidationRegistry
	{
		private static readonly ValidationRegistry<Type> registry = new ValidationRegistry<Type>(t => t, t =>
		                                                                                                 {
		                                                                                                 	List<IValidatorFactory> factories = new List<IValidatorFactory>();

		                                                                                                 	if (t.IsClass)
		                                                                                                 	{
		                                                                                                 		ITypeValidatorFactory factory =
		                                                                                                 			(ITypeValidatorFactory)
		                                                                                                 			Activator.CreateInstance(typeof(TypeValidatorFactory<>).MakeGenericType(t));
		                                                                                                 		if (factory.HasValidationMethods)
		                                                                                                 			factories.Add((IValidatorFactory)factory);
		                                                                                                 	}

		                                                                                                 	foreach (object attribute in t.GetCustomAttributes(typeof(ValidatorAttribute), true))
		                                                                                                 	{
		                                                                                                 		factories.Add(((ValidatorAttribute)attribute).Factory);
		                                                                                                 	}

		                                                                                                 	return factories;
		                                                                                                 });

		public static void ClearCache()
		{
			registry.ClearCache();
		}

		public static void RevertValidation(this Type type)
		{
			registry.RevertValidation(type);
		}

		public static void ReplaceValidation(this Type type, IEnumerable<IValidatorFactory> factories)
		{
			registry.ReplaceValidation(type, factories);
		}

		public static void ReplaceValidation<TValue>(this Type type, Action<TValue, IValidationContext> validateAction)
		{
			registry.ReplaceValidation(type, validateAction);
		}

		public static void AddValidation(this Type type, IEnumerable<IValidatorFactory> factories)
		{
			registry.AddValidation(type, factories);
		}

		public static void AddValidation<TValue>(this Type type, Action<TValue, IValidationContext> validateAction)
		{
			registry.AddValidation(type, validateAction);
		}

		public static IValidator ResolveValidator(this Type type)
		{
			return registry.ResolveValidator(type);
		}
	}
}